package sessionBean;

import interfaceBeanRemote.SessionConnexionRemote;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.persistence.*;

/**
 * Session Bean implementation class SessionConnexion
 */
@Stateless
public class SessionConnexion implements SessionConnexionRemote {

	@PersistenceContext(unitName = "EJB3_Entity")
	private EntityManager em;

	@PostConstruct
	public void init() {
		System.out.println("calling init method");
	}

	@PreDestroy
	public void cleanup() {
		System.out.println("calling cleanup method");
	}

	/**
	 * Default constructor.
	 */
	public SessionConnexion() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Object getUtilisateur(String login, String passWord, String nomTable) {
		Object o = null;
		try {
			String requete = "SELECT u FROM " + nomTable + " u "
					+ "WHERE u.login  = :loginUser "
					+ "AND u.passWord = :passWordUser";

			Query query = em.createQuery(requete);

			query.setParameter("loginUser", login);
			query.setParameter("passWordUser", passWord);

			o = query.getSingleResult();

			System.out.println("It 's good " + o.toString());

		} catch (IllegalStateException e) {
			System.out.println("Not result from getUser()");
		} catch (NoResultException e) {
			System.out.println("Not result2 from getUser()");
		}
		return o;
	}

}
